package com.book.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.core.domain.SysRole;
import com.book.core.domain.SysRoleUser;
import com.book.core.dto.SysUserGrantRoleDTO;
import com.book.core.enums.CommonStatus;
import com.book.core.query.SysRoleQuery;
import com.book.core.vo.PageVO;
import com.book.repository.SysRoleMapper;
import com.book.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hongmizfb
 * @description 针对表【sys_role(系统角色表)】的数据库操作Service实现
 * @createDate 2025-05-11 16:30:25
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {
    private final SysRoleUserServiceImpl sysRoleUserService;

    @Override
    public PageVO<SysRole> getSysRolePage(SysRoleQuery query) {
        String keyword = query.getKeyword();
        Page<SysRole> page = lambdaQuery()
                .eq(StrUtil.isNotBlank(keyword), SysRole::getRoleCode, keyword)
                .or().like(StrUtil.isNotBlank(keyword), SysRole::getRoleName, keyword)
                .or().like(StrUtil.isNotBlank(keyword), SysRole::getDescription, keyword)
                .or().eq(StrUtil.isNotBlank(keyword), SysRole::getRoleId, keyword)
                .page(query.toMpPage());
        return PageVO.of(page, SysRole.class);
    }

    @Override
    public List<SysRole> getRoleListByUserId(Integer userId) {
        List<Integer> roleList = sysRoleUserService.lambdaQuery()
                .eq(SysRoleUser::getUserId, userId).list().stream().map(SysRoleUser::getRoleId).collect(Collectors.toList());
        if (CollUtil.isEmpty(roleList)){
            return new ArrayList<>();
        }
        return lambdaQuery().in(SysRole::getRoleId, roleList).eq(SysRole::getStatus, CommonStatus.NORMAL.getCode()).list();
    }

    @Override
    public List<String> getRoleCodeListByUserId(Integer userId) {
        return getRoleListByUserId(userId).stream().map(SysRole::getRoleCode).collect(Collectors.toList());
    }

    @Override
    public boolean isAdmin(Integer userId) {
        return getRoleCodeListByUserId(userId).contains("admin");
    }

    @Override
    public List<Integer> getRoleIdListByUserId(Integer userId) {
        return sysRoleUserService.lambdaQuery()
                .eq(SysRoleUser::getUserId, userId).list().stream().map(SysRoleUser::getRoleId).collect(Collectors.toList());
    }

    @Override
    public void grantUserToRole(SysUserGrantRoleDTO dto) {
        List<Integer> roleIdList = dto.getRoleIdList();
        Integer userId = dto.getUserId();
        if (CollUtil.isNotEmpty(roleIdList) && userId != null) {
//            先删除该用户所有的角色，然后再新增
            sysRoleUserService.lambdaUpdate().eq(SysRoleUser::getUserId, userId).remove();
            for (Integer roleId : roleIdList) {
                sysRoleUserService.save(new SysRoleUser(roleId, userId));
            }
        }
    }
}




